package com.firesoon;

import net.bytebuddy.asm.Advice;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**
 * @author yakun.zeng
 * @version 1.0
 * @description: TODO
 * @date 2025/7/28 19:34
 */
public class ControllerTimerInterceptor {

    @RuntimeType
    public static Object intercept(@Advice.Origin Method method, @SuperCall Callable<?> callable) throws Exception {
        long start = System.currentTimeMillis();
        try {
            return callable.call();
        } finally {
            long end = System.currentTimeMillis();
            long duration = end - start;

            String controllerName = method.getDeclaringClass().getSimpleName();
            String methodName = method.getName();

            System.out.printf("[Controller Timer] %s.%s executed in %d ms%n",
                    controllerName, methodName, duration);
        }
    }
}
